// pages/invoice/choose-invoice/choose-invoice.js
var httpUtil = require('../../../utils/httpUtil.js');
var uiUtil = require('../../../utils/uiUtil.js');
const app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceLists: {
      totalCount: null,
      pageCount: null,
      pageNo: "0",
      pageSize: "30",
      dataList: []
    },

    requestGoodsTask: null, //请求task
    isLoadingMore: false,
    enableLoadMore: true,


    noInvoiceLists:{
      companyName:"不需要发票信息"
    },

    indexThat: null //发票选择

    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.getSearchBill();
  },

  /**
   * 我的发票查询
   */
  getSearchBill() {
    let pageNo = (parseInt(that.data.invoiceLists.pageNo) + 1).toString();
    let pageSize = that.data.invoiceLists.pageSize;
    let dataList = that.data.invoiceLists.dataList;
    let totalCount = that.data.invoiceLists.totalCount;

    var lastRequestTask = that.data.requestGoodsTask;
    if (lastRequestTask) {
      try {
        lastRequestTask.abort()
      } catch (e) { }
    }

    var curPage = that.data.invoiceLists.pageNo
    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载
      return;
    }

    var requestTask = httpUtil.PostRequest("searchBill", {
      pageNo: pageNo,
      pageSize: pageSize
    }, {
        success(res) {
          if (0 == that.data.invoiceLists.totalCount) {
            res.dataList = res.dataList || []
            if (res.dataList.length == 0) { //内容为空
              that.setData({
                fail: true
              })
            }
          } else {
            res.dataList = dataList.concat(res.dataList || []);
          }
          that.setData({
            invoiceLists: res,
            enableLoadMore: res.dataList.length < res.totalCount,
          })
        },
        fail(code, res) {
          that.setData({
            enableLoadMore: false,
          })
          uiUtil.showToast(res);
        },
        complete() {
          that.setData({
            isLoadingMore: false
          })
        }
      })
    that.setData({
      requestGoodsTask: requestTask
    })
  },

  /**
   * 我的发票删除
   */
  deleteInvoice(e) {
    let id = e.currentTarget.dataset.id;
    httpUtil.PostRequest("delBill", {
      id: id
    }, {
        success(res) {
          that.onResultRegisterSuccess();
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },

  

  /**
   * 发票信息选择
   */
  indexChange(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    that.setData({
      indexThat: index
    })
    setTimeout(function () {
      var pages = getCurrentPages();
      var lastPage = pages[pages.length - 2];
      console.log(lastPage)
      if (lastPage.route == "pages/index/order/order-pre/order-pre") {
        lastPage.setData({
          "value.invoice": item
        })
        wx.navigateBack({
          delta: 1,
        });
      }
    }, 300)
  },


  /**
  * 触底加载
  */
  onReachBottom() {
    that.getSearchBill();
  },


  /**
  * 回调
  */
  onResultRegisterSuccess() {
    that.setData({
      invoiceLists: {
        totalCount: null,
        pageCount: null,
        pageNo: "0",
        pageSize: "30",
        dataList: []
      },
      requestGoodsTask: null, //请求task
      isLoadingMore: false,
      enableLoadMore: true
    })
    that.getSearchBill();
  }
})